﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XH.BMS.Models;

namespace XH.BMS.ViewModels
{
    public class TrendViewModel
    {
        private GroupItemModel _currentGroup;
        // 不需要代码方式修改，不需要通知页面，只需要获取页面中操作的变化
        public GroupItemModel CurrentGroup
        {
            get { return _currentGroup; }
            set
            {
                _currentGroup = value;
                Refresh();
            }
        }

        public List<TrendPropModel> PropList { get; set; }

        public ObservableCollection<ObservableCollection<double>> ChartValues { get; set; } = new ObservableCollection<ObservableCollection<double>>();

        public TrendViewModel()
        {
            PropList = new List<TrendPropModel>();
            PropList.Add(new TrendPropModel { Header = "电量", PropName = "CapacitanceHistory", CheckStateChanged = Refresh });
            PropList.Add(new TrendPropModel { Header = "温度", PropName = "TemperatureHistory", CheckStateChanged = Refresh });
            PropList.Add(new TrendPropModel { Header = "SoC", PropName = "" });
            PropList.Add(new TrendPropModel { Header = "SoH", PropName = "" });
            PropList.Add(new TrendPropModel { Header = "电压", PropName = "" });
            PropList.Add(new TrendPropModel { Header = "电流", PropName = "" });
        }

        private void Refresh()
        {
            // 等同于清除图表序列
            ChartValues.Clear();
            var pl = PropList.Where(p => p.IsSelected).ToList();
            for (int i = 0; i < pl.Count; i++)
            {
                var pi = CurrentGroup.GetType().GetProperty(pl[i].PropName);
                // 反射？这里不能用反射，反射是创建一个新对象  
                if (pl[i].Header == "电量" && pi != null)
                {
                    ChartValues.Add(CurrentGroup.CapacitanceHistory);
                }
                if (pl[i].Header == "温度" && pi != null)
                {
                    ChartValues.Add(CurrentGroup.TemperatureHistory);
                }
            }
        }
    }
}
